/**
 * Created by Mikey on 2017/6/13.
 */
var vm;
initVue();

function initVue() {
    vm = new Vue({
        el:"#app",
        data : {
            books : [],
            type : "",
            btypes : [],
            bstatuss : [],
            pageCount : 0,
            activeNumber : 1,
            limit : 5,
            offset : 0
        },
        methods : {
            selectPageCount:function (e) {
                if($(e.currentTarget).is(".active")){
                    $('.pagination a').removeClass("active");
                }else{
                    $('.pagination a').removeClass("active");
                    $(e.currentTarget).toggleClass("active");
                }
                this.activeNumber = $(e.currentTarget).html();
                this.getBookList();
            },
            getBookList : function () {
                this.offset = (this.activeNumber - 1)*(this.limit);
                $.ajax({
                    url : "/getBookList.json?limit="+this.limit+"&offset="+this.offset,
                    type : "GET",
                    success : function (ret) {
                        if(ret.msg == "success"){
                            vm.books = ret.data.books;
                            vm.pageCount =Math.ceil(ret.data.count/vm.limit);
                        }else{
                            console.log(ret.msg)
                        }
                    }
                })
            },
            dialog : function (e){
                this.selectBookType();
                this.selectBookStatus();
                var modal = $(e.currentTarget);
                if(modal.attr("data-whatever") == "添加"){
                    $('form input').val("");
                    $('#exampleModal').modal()
                }else if(modal.attr("data-whatever") == "修改"){
                    if($('.hang').is(".success")){
                        var books = this.books;
                        var book = books[$('.success').attr("data-index")];
                        $('#b_id').val(book.b_id);
                        $('#b_name').val(book.b_name);
                        $('#b_author').val(book.b_author);
                        $('#b_type').val(book.b_type);
                        $('#b_status').val(book.b_status);
                        $('#exampleModal').modal();
                    }else{
                        alert("请选择数据")
                    }
                }
                this.type = modal.attr("data-whatever");
            },
            selectBookType : function () {
                $.ajax({
                    url : "/selectDictByDpid.json",
                    type : "GET",
                    data : {
                        d_pid : "tslx"
                    },
                    success : function (ret) {
                        if(ret.msg == "success"){
                            vm.btypes = ret.data;
                        }else{
                            alert(ret.msg)
                        }
                    }
                })
            },
            selectBookStatus : function () {
                $.ajax({
                    url : "/selectDictByDpid.json",
                    type : "GET",
                    data : {
                        d_pid : "tszt"
                    },
                    success : function (ret) {
                        if(ret.msg == "success"){
                            vm.bstatuss = ret.data;
                        }else{
                            alert(ret.msg)
                        }
                    }
                })
            },
            selectRow : function (e) {
                if($(e.currentTarget).is(".success")){
                    $('.hang').removeClass("success");
                }else{
                    $('.hang').removeClass("success");
                    $(e.currentTarget).toggleClass("success");
                }
            },
            send : function (e) {
                if($(e.currentTarget).text() == "删除"){
                    if($('.hang').is(".success")){
                        var books = this.books;
                        $.ajax({
                            url : "/deleteBook.json",
                            type : "POST",
                            data : {
                                b_id : books[$('.success').attr("data-index")].b_id
                            },
                            success : function (ret) {
                                if(ret.msg == "success"){
                                    location.reload()
                                }else{
                                    alert(ret.msg)
                                }
                            }
                        })
                    }else{
                        alert("请选择数据")
                    }
                }else{
                    $.ajax({
                        url : "/addOrEditBook.json",
                        type : "POST",
                        data : {
                            b_id : $('#b_id').val(),
                            b_name : $('#b_name').val(),
                            b_author :  $('#b_author').val(),
                            b_type : $('#b_type').val(),
                            b_status : $('#b_status').val()
                        },
                        success : function (ret) {
                            if(ret.msg == "success"){
                                location.reload()
                            }else{
                                alert(ret.msg)
                            }
                        }
                    })
                }
            }
        },
        mounted : function () {
            this.getBookList();
        }
    })
}